package com.eastmoney.emis.edda.config;

import com.alibaba.ttl.threadpool.TtlExecutors;
import com.eastmoney.emis.edda.infrastructure.bankname.BankNameService;
import com.eastmoney.emis.utils.common.exception.PrintExceptionInfo;
import com.eastmoney.emis.utils.common.utils.RequestUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


@Configuration
public class StartUp {

    private static final Logger log = LoggerFactory.getLogger(StartUp.class);
    @Autowired
    private Environment environment;

    @PostConstruct
    public void init() {
        config();
    }

    public void config() {
        //获取环境变量，初始化服务器端IP
        RequestUtil.setServerIp(environment.getProperty("JAVA_LOCALIP"));
    }

    @Bean
    public CommandLineRunner commandLineRunner(BankNameService bankNameService) {
        return args -> {
            bankNameService.loadBankMultiLanguageName();
            ScheduledExecutorService service = TtlExecutors.getTtlScheduledExecutorService(Executors.newScheduledThreadPool(2));
            service.scheduleAtFixedRate(() -> {
                try {
                    bankNameService.initEnglishNameMap();
                } catch (Exception e) {
                    log.error(PrintExceptionInfo.printErrorInfo(e));
                }
            }, 5, 1800, TimeUnit.SECONDS);

        };
    }

}
